﻿#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <easyx.h>
#include <time.h>
#include <conio.h>
#include "vector.h"
#include <assert.h>
#include <math.h>

#define Length 800
#define Width 700
//调整难度
#define boardx -Length*0.375/2
#define boardy Length*0.375/2

#define velocity 10//速度
#define volume 200//砖块数量
typedef struct Ball
{
	int x;
	int y;
	int vx;
	int vy;
}BALL;
//砖块
typedef struct Blick
{
	int x;
	int y;
	//char direction;
	int blickstatus;
}Blick;
//调用函数时传过来要创建的数量
//创建
void BlickFound(Blick*blicks,int x)
{
	for (int i = 0; i < volume; i++)
	{
		blicks[i].x = -380+i*20;
		blicks[i].y = 280;
	}
	
}

void BlickCreat(Blick*blicks,int x)
{
	for (int i = 0; i < x; i++)
	{
		int left = blicks[i].x - 10;
		int top = blicks[i].y + 10;
		int right = blicks[i].x + 10;
		int bottom = blicks[i].y - 10;
		fillrectangle(left, top, right, bottom);
	}
}

//判断距离，从而消除该方块。
//优化

int main()
{

	initgraph(Length, Width);
	setbkcolor(RGB(150, 205, 205));
	cleardevice();
	setorigin(Length/2, Width/2);
	setaspectratio(1, -1);
	int R = 10;
	BALL ball;
	Blick blicks[volume];//砖块数组
	int x = 20;
	BlickFound(blicks,x);
	srand(time(NULL));
	int m = -350;
	int n = 350;
	ball.x = rand() % (n - m + 1) + m;
	m = -250;
	n = 250;
	ball.y = rand() % (n - m + 1) + m;
	ball.vx = velocity;
	float rleft = boardx, rtop = -(Width / 2) + 20;
	float rright = boardy, rbottom = -(Width / 2);
	if (ball.x % 2 == 0)
	{
		ball.vx = -ball.vx;
	}
	ball.vy = velocity;
	if (ball.y % 2 == 0)
	{
		ball.vy = -ball.vy;
	}
	while (1)
	{
		cleardevice();
		setfillcolor(BLACK);
		solidcircle(ball.x,ball.y, R);
		setfillcolor(RGB(250, 235, 215));
		solidrectangle(rleft, rtop, rright, rbottom);
		BlickCreat(blicks,x);

		Sleep(50);
		ball.x += ball.vx;
		ball.y += ball.vy;
		if (ball.x > Length/2 - R || ball.x < -(Length/2) + R)
		{
			ball.vx = -ball.vx;
		}
		if (ball.y > (Width / 2 - R))
		{
			ball.vy = -ball.vy;
		}
		//判断玩家是否控制
		if (_kbhit() != 0)
		{
			char c = _getch();
			if (c == 'a')
			{
				if (rleft > -Length / 2)
				{
					rleft -= 20;
					rright -= 20;
				}
			}
			if (c == 'd')
			{	
				if (rright < Length / 2)
				{
					rleft += 20;
					rright += 20;
				}
			}
		}
		//判断是否与挡板相撞
		if (rleft <= ball.x && ball.x <= rright && ball.y <= rtop + R)
		{
			ball.vy = -ball.vy;
		}
		for (int i = 0; i < volume; i++)
		{
			if (sqrt(pow((blicks[i].x - ball.x), 2) + pow((blicks[i].y - ball.y),2)) < 24)
			{
				
				if (abs(blicks[i].y - ball.y)<17)
				{
					ball.vx = -ball.vx;
				}
				if (abs(blicks[i].x - ball.x) < 17)
				{
					ball.vy = -ball.vy;
				}

				blicks[i].x = 0;
				blicks[i].y = 0;
			}
		}
		if (ball.y < -300)
		{
			
		}
	}
	getchar();
	return 0;
}